package main

import (
	"fmt"
	"github.com/spf13/pflag"
	"github.com/spf13/viper"
)

func main() {
	pflag.String("ip", "127.0.0.1", "Server running address")
	pflag.Int64("port", 8080, "Server running port")
	pflag.Parse()
	viper.BindPFlags(pflag.CommandLine)
	fmt.Printf("ip :%s , port:%s \n", viper.GetString("ip"), viper.GetString("port"))
	// 一般使用os包获取环境变量，os.Getenv("JAVA_HOME")
	//viper 预加载匹配的环境变量
	viper.AutomaticEnv()
	if env := viper.Get("JAVA_HOME"); env == nil {
		println("error when get viper_env")
	} else {
		fmt.Printf("viper_env 'JAVA_HOME' = %#v\n", env)
	}
}
